<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
<!--        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>-->
        <el-form-item>
          <el-button v-perm="'employeeLog/add'" type="primary" @click="showConfirmation">创建日志</el-button>
        </el-form-item>
<!--        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="customerLists" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange"  stripe style="width: 100%;" border>
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="companyName" label="所属人" width="100"  >
      </el-table-column>
      <el-table-column prop="success" label="今日已完成事项" width="150" >
      </el-table-column>
      <el-table-column prop="incomplete" label="今日未完成事项" width="150" >
      </el-table-column>
      <el-table-column prop="help" label="需要同事帮助" width="150" >
        <template slot-scope="scope">
          {{ scope.row.help === 0 ? '需要同事帮助' : '自己能独立完成' }}
        </template>
      </el-table-column>
      <el-table-column prop="development" label="每天是否在成长" width="150" >
        <template slot-scope="scope">
          {{ scope.row.development === 0 ? '今天收获满满' : '今天没有收获' }}
        </template>
      </el-table-column>
      <el-table-column prop="analysis" label="客户情况分析" width="150" >
      </el-table-column>
      <el-table-column prop="summarize" label="工作总结" width="150" >
      </el-table-column>
      <el-table-column prop="plan" label="明日工作计划" width="150" >
      </el-table-column>
      <el-table-column prop="submissionDate" label="提交日期" width="100" :formatter="formatDate" >
      </el-table-column>

      <el-table-column label="操作" width="150":align="'center'">
        <template scope="scope">
          <el-button v-perm="'employeeLog/upd'" type="info" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button v-perm="'employeeLog/del'" type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    工具条-->
    <el-col :span="24" class="toolbar">
<!--      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :current-page="currentPage" :page-size="pageSize" :total="totals" style="float:right;">
      </el-pagination>
    </el-col>
    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="150px" :rules="editFormRules" ref="editForm">
        <el-form-item label="今日完成事项">
          <el-input v-model="editForm.success" auto-complete="off" type="textarea"style="font-family: 'Arial'; font-size: 14px; color: #333;" ></el-input>
        </el-form-item>
        <el-form-item label="今日未完成事项">
          <el-input v-model="editForm.incomplete" auto-complete="off" type="textarea"style="font-family: 'Arial'; font-size: 14px; color: #333;"></el-input>
        </el-form-item>
        <el-form-item label="需要同事帮助">
          <el-select v-model="editForm.help" placeholder="请选择">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="每天是否在成长">
          <el-select v-model="editForm.development" placeholder="请选择">
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作总结">
          <el-input v-model="editForm.summarize" auto-complete="off" type="textarea" style="font-family: 'Arial'; font-size: 14px; color: #333;"></el-input>
        </el-form-item>
        <el-form-item label="客户情况分析">
          <el-input v-model="editForm.analysis" auto-complete="off" type="textarea" style="font-family: 'Arial'; font-size: 14px; color: #333;"></el-input>
        </el-form-item>
        <el-form-item label="明日工作计划">
          <el-input v-model="editForm.plan" auto-complete="off" type="textarea" style="font-family: 'Arial'; font-size: 14px; color: #333;"></el-input>
        </el-form-item>
        <el-form-item label="提交时间" v-show="false">
          <template slot-scope="scope">
            {{ getCurrentDateTime() }}
          </template>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>
<script>
import util from '../../common/js/util'


export default {
  data() {
    return {

      //1.分页查询的相关数据
      customerLists: [],
      total: 0,
      currentPage: 1,
      pageSize: 5,
      listLoading: false,
      sels: [],//列表选中列

      //2.高级查询相关数据
      keyword: '',

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
      },
      //编辑界面数据
      editForm: {
        id:null,
        userid:'',
        success:'',
        incomplete:'',
        help:'',
        development:'',
        summarize:'',
        plan:'',
        submissionDate:'',
        analysis:'',
      },
       //业务员
         salesMan:[],
    }
  },
  methods: {
    formatDate(row) {
      const date = new Date(row.submissionDate);
      const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
      return date.toLocaleDateString('zh-CN', options).replace(/\//g, '-');
    },
    getCurrentDateTime() {
      const currentDateTime = new Date().toISOString(); // 获取当前时间
      this.addForm.submissionDate = currentDateTime; // 设置到提交时间数据项中
      return currentDateTime; // 返回当前时间以显示
    },

    //点击新建日志时，弹出提示框提示用户是否新建日志
    showConfirmation() {
      this.$confirm('确定要创建新的员工日志吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 用户点击确定时执行的逻辑
        this.$router.push('/addemployeelog'); // 执行页面跳转
      }).catch(() => {
        // 用户点击取消时执行的逻辑仍在当前页面；
      });
    },

    //获取业务员
    getSalesman() {
      this.$http.get("/customSalesman").then(res=>{
        this.salesMan = res.data;
      })
    },
    //1.分页获取日志列表
    getcustomerLists() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;
      this.$http.post("/customEmployeelog", para).then((res) => {
        this.customerLists = res.data.records;
        this.totals = res.data.total;
        this.listLoading = false;
      }).catch(res => {
        this.$message.warning("系统繁忙，请稍后重试!!!")
      });
    },

    //2.点击分页实现
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getcustomerLists();
    },

    //3.关键字查询
    keywordQuery() {
      //假如当前页不在第一页，防止高级查询没有那么多页的时候出现空数据的问题
      this.currentPage = 1;
      this.getcustomerLists();
    },
    //5.删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/customEmployeelog/" + row.id).then(res => {
          if (res.data.success) {
            //计算总页数
            let totalPage = Math.ceil(this.total / this.pageSize);
            // 当删除最后一页的最后一条数据，并且当前不是第一页，就让当前页-1
            if (this.currentPage == totalPage && (this.total - 1) % this.pageSize == 0 && this.currentPage != 1) {
              this.currentPage = this.currentPage - 1;
            }
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getcustomerLists();
          } else {
            this.$message.warning("系统繁忙，请稍后重试!!!");
          }
          this.listLoading = false;
        }).catch(res => {
          this.$message.warning("系统繁忙，请稍后重试!!!");
        })
      }).catch(() => {
      });
    },

    //6.选项改变时触发
    selsChange: function (sels) {
      this.sels = sels;
    },

    //7.批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/customEmployeelog", ids).then(res => {
          if (res.data.success) {
            //处理当前页
            //算总页数
            let totalPage = Math.ceil(this.total / this.pageSize);
            //当删除最后一页的所有数据，并且不是第一页，就让当前页-1
            if (this.currentPage == totalPage && (this.total - this.sels.length) % this.pageSize == 0 && this.currentPage != 1) {
              this.currentPage = this.currentPage - 1;
            }
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getcustomerLists();
          } else {
            this.$message.warning("系统繁忙，请稍后重试!!!");
          }
          this.listLoading = false;
        }).catch(res => {
          this.$message.warning("系统繁忙，请稍后重试!!!");
        })
      }).catch(() => {
      });

    },

    //8.显示编辑界面
    handleEdit: function (index, row) {
      this.editForm = Object.assign({}, row);

      this.editFormVisible = true;
      // 转换“是”或“否”为后端需要的“0”或“1”
      this.editForm.help = row.help === 0 ? '是' : '否';
      this.editForm.development = row.development === 0 ? '是' : '否';

    },
    // 在编辑界面中的提交方法中，调整转换逻辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
          /*  // 获取当前时间
            const currentDate = new Date();
            const formattedDate = currentDate.toISOString(); // 或者使用其他格式化方法将日期转换为适当的字符串格式

            // 更新提交时间为当前时间
            this.addForm.submissionDate = formattedDate;*/

            let para = Object.assign({}, this.editForm);
            // 将“是”或“否”转换为“0”或“1”
            para.help = this.editForm.help === '0' ? 0 : 1;
            // 将“是”或“否”转换为“0”或“1”
            para.development = this.editForm.development === '0' ? 0 : 1;

            // 这里是针对日期格式转换
            para.submissionDate = new Date().toISOString(); // 或者使用其他格式化方法将日期转换为适当的字符串格式
            this.$http.put("/customEmployeelog", para).then(res => {
              if (res.data.success) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['editForm'].resetFields();
              } else {
                this.$message.warning("系统繁忙，请稍后重试!!!");
              }
              this.editLoading = false;
              this.editFormVisible = false;
              this.getcustomerLists();
            });
          });
        }
      });
    },
  },
  mounted() {
    this.getcustomerLists();

  }
}

</script>

<style scoped>

</style>